<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title></title>
<script src="/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    $(function(){
        let club, roomid,seatIndex, userId,lastMopai,endAccount;
        var socket = io("http://ygame-test1.shenghan.net.cn:32510"); //通过ip和端口建立一个socket client
        var socket = io("http://101.201.34.22:32510"); //通过ip和端口建立一个socket client
        var hash = window.location.hash, inter;
        socket.on('connect', function(){
            console.log('connect a socket client');
        });
        let successNum = 0;
        let emitNum = 0,success = false;
        // var a = setInterval(function(){
        //     for(let i = 0; i < 1000; i++) {
        //         socket.emit("beat",{
        //             time : "xxxx-xxxx-xxxx"
        //         })
        //     }
        //     emitNum += 1000;
        //     console.log("发送数量："+emitNum);
        //     console.log("接收数量:"+successNum);
        //     if(emitNum >=50000){
        //         // console.log("------一万次心跳----------");
        //         // console.log(successNum);
        //         // console.log(emitNum);
        //         success = true;
        //         clearInterval(a)
        //     }
            
        // },1000)
        socket.on("pong_error", function(){
            successNum ++
            if(success) {
                console.log(successNum);
            }
        })
        socket.on("pong", function(){
            successNum++;
            if(success) {
                console.log(successNum);
            }
        })
        return;

        if(!hash){
            let btn = $("#btn")
            btn.click(function(){
                btnClick();
            });
        }else{
            hash = hash.substring(1, hash.length)
            let startAccount = +hash.split("_")[0];
            endAccount= +hash.split("_")[1]
            startAccount ++;
            $("#userId").val(startAccount)
            btnClick();
        }
        function btnClick(ret){
            account = +$("#userId").val();
            if(!endAccount) endAccount = account+23;
            // socket.emit("open", {userId:account})
            // socket.emit("beat", {})
            inter = setInterval(function(){
                socket.emit("beat", {})
            },2000)
            $.ajax({
                url : "/user",
                type :"post",
                data : {
                    account : account
                },
                success : function(data){
                    if(data.userId){
                        //连接
                        userId = data.userId
                        socket.emit("open", {userId:data.userId})
                        console.log("------------open---------");
                        if(account< endAccount && !ret){
                            setTimeout(function(){
                                window.open("http://39.105.223.74:3000/test#"+account+"_"+endAccount,'_blank');
                            },2000)
                        }
                        socket.emit("beat", {})
                        setTimeout(function(){
                            socket.emit("beat", {})
                        })
                    }else{
                        // alert("用户不存在");
                    }
                }
            })
        }
        socket.on("pong", function(data){
        })
        socket.on("pong_error", function(){
            // clearInterval(inter)
            // btnClick(true);
            clearInterval(inter)
            btnClick(true);
            console.log("--------------pong_error-------------");
        });
        socket.on("open_result", function(data){
            console.log("open_result");
            if(data.roomId){
                roomid = data.roomId;
            }
            if(roomid){
                console.log("----------ready------------");
                socket.emit("ready",{})
            }else{
                console.log("----------get_club------------");
                socket.emit("get_club", {})
            }
        })

        //获取亲友圈
        socket.on("get_club_result", function(data){
            club = data.clubsData[0]
            if(!club){
                return;
                // return alert("没有加入俱乐部");
            }
            socket.emit("club_tables", {clubId: club.club_id});
        })
        //获取亲友圈桌子
        socket.on("club_tables_result", function(data){
            // alert("获取俱乐部桌子");
            club.tables = data.tables;
            //获取可以用的桌子，并坐下
            if(!roomid){
                for(let i = 0; i < club.tables.length; i++) {
                    let tab = club.tables[i];
                    let canJoin = false;
                    for(let i = 0; i < 4; i++){
                        let seat = tab.seats[i];
                        if(seat.userId ==0) {
                            //进入房间坐下
                            roomid = tab.id;
                            seatIndex = i;
                            // alert(seatIndex);
                            socket.emit("login", {roomid:roomid, userId:userId});
                            canJoin = true;
                            break;
                        }
                    }
                    if(canJoin) {
                        break;
                    }
                }
            }
        })
        socket.on("game_sync_push", game_sync_push)

        function game_sync_push(data){
            console.log("game_sync_push:"+userId);
            let seats = data.seats;
            let seat;
            for(let i = 0; i < data.seats.length; i++) {
                let st = data.seats[i];
                if(st.userid == userId) {
                    seat = data.seats[i];
                }
            }

            // let seat = data.seats.find(function(st){
            //     st.userid == userId;
            // })
            // let seat = data.seats.find((st) => st.userid == userId);
            if(seat.canChupai){
                let pai = seat.holds.pop();
                socket.emit("chupai", {pai:pai})
            }
        }
        socket.on("login_result", function(data){
            console.log("login_result");
            // alert(JSON.stringify(data));
            socket.emit("take_seat", {
                userName :"abc",
                roomId : roomid,
                seatIndex : seatIndex
            });
        });


        socket.on("take_seat_result", function(data){
            console.log("坐下通知");
            console.log(data);
            
        })

        //游戏开局事件
        socket.on("game_result", function(data){
            console.log("game_result");
            console.log(data);
            let results = data.ret;
            for(let i = 0; i < results.length; i++) {
                let event = results[i].event;
                if(event == "game_mopai_push"){
                    lastMopai = results[i].data.pai;
                    console.log("----------摸到的牌----------------:"+lastMopai);
                    // socket.emit("chupai",{
                    //     pai : lastMopai
                    // });
                }
                if(event == "game_action_push"){
                    socket.emit("guo")
                }
                if(event=="game_sync_push"){
                    game_sync_push(results[i].data);
                }
            }
        })

        socket.on("game_action_push", function(data){
            socket.emit("guo")
        })

        socket.on("game_chupai_push", function(data){
            console.log("game_chupai_push:"+lastMopai);
            if(data.userId == userId){
                setTimeout(function(){
                    socket.emit("chupai",{
                        pai : lastMopai
                    });
                },500)
            }
        });

        socket.on("guo_notify_push", function(){
            setTimeout(function(){
                socket.emit("chupai",{
                    pai : lastMopai
                });
            }, 500)
        })
        
        socket.on("game_over_push", function(data){
            console.log("game_over_push");
	    console.log(data.endinfo);
	    if(data.endinfo){
                roomid = null;
		setTimeout(function(){
		    socket.emit("get_club",{})

		},Math.round()*100000);
	    }else{
		btnClick(true)	
	    }
        })

        socket.on("user_ready_push", function(data){
        })
        socket.on("game_chupai_notify_push", function(data){
        })

        function chupai(){

        }
    })
</script>
</head>
<body>
    <input type="text" name="userId" id="userId"> <input type="button" value="登陆" id="btn">
</body>
</html>
